package com.swjd.sbm.controller;

import com.swjd.sbm.service.PinglunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class PingLunController {

    @Autowired
    private PinglunService pinglunService;

    @RequestMapping("getpinglun")
    public Map<String,Object> getpinglun(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,String> reqbody) {
        response.setContentType("text/html;charset=utf-8");
        /* 设置响应头允许ajax跨域访问 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 星号表示所有的异域请求都可以接受， */
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        int page = Integer.parseInt(reqbody.get("page"));
        int pagesize = Integer.parseInt(reqbody.get("pagesize"));

        String order = reqbody.get("pagesize");

        int tid = Integer.parseInt(reqbody.get("id"));

        System.out.println(tid);
        List<Map<String,Object>> list =pinglunService.getpinglun((page-1)*pagesize,page*pagesize,tid);
        int pagess = pinglunService.pagesize(tid);
        Map<String,Object> map = new HashMap<>();
        map.put("replylist",list);
        map.put("fetchPageNum",pagess);
        map.put("statusCode",200);
        return map;
    }


    @RequestMapping("setpinglun")
    public Map<String,Object> setpinglun(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,String> reqbody) {
        response.setContentType("text/html;charset=utf-8");
        /* 设置响应头允许ajax跨域访问 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 星号表示所有的异域请求都可以接受， */
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");


        String text = reqbody.get("text");
        String id = reqbody.get("id");
        String token = reqbody.get("token");
        String shebei = reqbody.get("shebei");
        System.out.println(text+id+token+shebei);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        int list = pinglunService.addp(id,text,date);

        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        data.put("code",0);
        data.put("msg","msg");

        map.put("statusCode",200);
        return map;
    }


}
